/**
 * Created by GYL on 2018/7/25
 */
import React, { Component } from 'react';
import {TreeSelect,Select , Row, Col, Tabs, Card, InputNumber, Popover, Tag, Checkbox, Collapse,Table,Button,message,DatePicker  } from 'antd';
import Container from '../../components/common/Container';
import MyTable from '../../components/common/MyTable';
import PublicService from '../../services/PublicService';
import request from '../../utils/request';
import style from './WordEdit.less';
import moment from 'moment';
const Panel = Collapse.Panel;
const Option = Select.Option;
class WordEdit extends Component {

    constructor(props) {
        super(props)
        this.columns = [
            {
                title: '序号',
                dataIndex: 'num',
                width: '5%',
                align: "center",
                key: "1"
            },
            {
                title: '外勤任务',
                dataIndex: 'taskName',
                width: '15%',
                align: "center",
                key: "2",

            },
            {
                title: '任务类型',
                dataIndex: 'taskType',
                width: '15%',
                key: "3",
                align: "center",
                render: (text, record) => {
                    return <Select size="small" style={{width:"100%",border:0}} defaultValue={text == 1?"安全检查":"安全考核"} style={{ width: 120 }} onChange={this.onSelect.bind(this,record)}>
                    <Option value="安全检查">安全检查</Option>
                    <Option value="安全考核">安全考核</Option>
                  </Select>

                }

            },
            {
                title: '预计完成时间',
                dataIndex: 'willFinishTime',
                width: '10%',
                key: "4",
                align: "center",
                render: (text, record) => {
                    return <DatePicker size="small" onChange={this.onDatePicker.bind(this,record)} defaultValue={text}/> 

                }

            },
            {
                title: '操作',
                width: '10%',
                dataIndex: 'number145',
                align: "center",
                key: "5",
                render: (text, record) => {
                    return <Tag color="#108ee9" onClick={this.onDelete.bind(this,record)}>删除</Tag>

                }
            }
        

        ];

    }


    state = {
        treeData: [
        ],
        loading: false,
        selectedRowKeys: [],
        selectedRows: [],
        dataSource: [],
        pageIndex: 1,
        pageSize: 10,
        idList: [],
        name:"",
        addArr:[],
        GroupArrMo:[],
        dateText:moment()
    }

    componentDidMount() {
        request({
            url: `/zhrq-web/gas/gaspandect/getGasSiteList`,
            method: 'POST',
            data: {}
        }).then(data => {
            console.log(data)
            if (data.rc === 0) {

                this.setState({
                    treeData: data.ret.list
                })

               
            }
        })

        request({
            url: `/zhrq-web/patrol/patroltask/getPageList`,
            method: 'POST',
            data: {patrolPersonnelId:PublicService.getCookie('patrolPersonnelId'),takeTime:PublicService.getCookie('WorkAlltakeTime')}
        }).then(data => {
            if(data.rc == 0) {
                console.log(data)
                data.ret.items.forEach((item) => {
                    item.willFinishTime = moment(item.willFinishTime)
                })
                this.setState({
                    dataSource:data.ret.items,
                    name:PublicService.getCookie('patrolPersonnelName')
                })
            }
        })




    }

    //删除按钮
    onDelete = (record) => {
       let arr = this.state.dataSource

        arr.forEach((item,index) => {
            if(item.num == record.num){
                arr.splice(index,1)
            }
        })

        this.state.addArr.forEach((item,index) => {
            if(item.taskName == record.taskName) {
                this.state.addArr.splice(index,1)
            }
        })



        setTimeout(() => {
          this.setState({
            dataSource:arr
          })
        },200)

        if(record.id) {
            request({
                url: `/zhrq-web/patrol/patroltask/deleteList`,
                method: 'POST',
                data: [{id:record.id}]
            }).then(data => {
                console.log(data)
                if(data.rc == 0) {
                    message.success("删除成功")
                }
            })
        }
    }

    //下拉选择
    onSelect = (v,record) => {
        console.log(v,record)
        if(record == "安全检查") {
            record = 1
        }

        if(record == "安全考核") {
            record = 2
        }
        v.taskType = record

        this.state.dataSource.forEach((item) => {
            if(item.num === v.num) {
                item = v
            }
        })

        this.state.addArr.forEach((item) => {
            console.log(item,v)
            if(item.taskName == v.taskName) {
                item.taskType = v.taskType
            }
        })


    }

    //时间选取
    onDatePicker = (record,v,b) => {
        console.log(record,v,b)
        this.state.dataSource.forEach((item) => {
            if(item.num === record.num) {
                item.willFinishTime = moment(v)
            }
        })

        // this.state.addArr.forEach((item) => {
        //     console.log(item,v)
        //     if(item.taskName == v.taskName) {
        //         item.taskType = v.taskType
        //     }
        // })
    }

    onA = () => {
        this.props.history.goBack()
    }

    onGroupMo = (checkedValues,v) => {
        console.log(checkedValues,v)
        this.setState({
            GroupArrMo: checkedValues
        })
    }


    //确认添加按钮
    onAdd = () => {
        console.log(this.state.GroupArrMo)
        let arr = this.state.dataSource
        this.state.GroupArrMo.forEach((item,index) => {
            arr.push({
                taskName:item,
                baseUserId:1,
                patrolPersonnelId:PublicService.getCookie('patrolPersonnelId'),
                takeTime:moment().format("YYYY-MM-DD HH:mm:ss"),
                taskType:1,
                willFinishTime:moment(),
            })

            this.state.addArr.push({
                key:index+Math.random(),
                taskName:item,
                baseUserId:1,
                patrolPersonnelId:PublicService.getCookie('patrolPersonnelId'),
                takeTime:moment().format("YYYY-MM-DD HH:mm:ss"),
                taskType:1,
                willFinishTime:moment(),
            })

            setTimeout(() => {
               this.setState({
                dataSource:arr,
              
               })
            },200)
        })
       
    }

   




    //保存按钮 
    save = () => {
        console.log(this.state.dataSource,this.state.addArr)
        let dataid = []
        let dataNoid = []
       let arrAll = []
        this.state.dataSource.forEach((item) => {
            if(item.id) {
                dataid.push({
                    id:item.id,
                    baseUserId:item.baseUserId,
                    patrolPersonnelId:PublicService.getCookie('patrolPersonnelId'),
                    takeTime: moment(item.takeTime).format("YYYY-MM-DD HH:mm:ss"),
                    taskName:item.taskName,
                    taskType:item.taskType,
                    willFinishTime:moment(item.willFinishTime).format("YYYY-MM-DD HH:mm:ss"),
                })
            } else {
                dataNoid.push({
                    baseUserId:item.baseUserId,
                    patrolPersonnelId:PublicService.getCookie('patrolPersonnelId'),
                    takeTime:item.takeTime,
                    taskName:item.taskName,
                    taskType:item.taskType,
                    willFinishTime:moment(item.willFinishTime).format("YYYY-MM-DD HH:mm:ss"),
                })
            }
        })
        arrAll = [...dataid,...dataNoid]
        setTimeout(() => {
            console.log(dataid,dataNoid,arrAll)
            request({
                url: `/zhrq-web/patrol/patroltask/insertList`,
                method: 'POST',
                data: arrAll
            }).then(data => {
                if(data.rc == 0) {
                    message.success("添加成功")
                    this.setState({
                        addArr:[],
                        GroupArrMo:[]
                    })
    
                    setTimeout(() => {
                        this.onA()
                    },1000)
                }
                console.log(data)
            })
        },1000)



    }

    onTreeSelect = (v, b, n) => {
        console.log(v, b, n)
        this.setState({
            GroupArrMo: b,
          
            value: v
        })
    }

    render() {
        const {loading, selectedRowKeys, dataSource, pageIndex, pageSize, name, number116, number145, number500 ,treeData} = this.state
        const windowWidth = document.body.clientWidth;
        const columns = this.columns
        const height = document.body.clientHeight;
        let containerHeight = '88vh';
        if (windowWidth > 1367) {
            containerHeight = '88vh';
        }
        console.log(dataSource)
        return (
            <div>
                <Container
                    style={{ height: containerHeight, position: "relative", marginTop: "1vh", padding: "0px 10px" }}
                    header={true}
                >
                    <Row>
                        <Col span={12}><a onClick={this.onA}>外勤任务管理</a> >> 编辑任务</Col>
                        <Col span={12}><a onClick={this.onA} style={{ float: "right" }}>返回</a></Col>
                    </Row>
                    <p>
                        外勤人员: {this.state.name}
                    </p>


                    <p style={{ paddingBottom: "-3px" }}>

                            <Row style={{ paddingBottom: "-3px" }}>
                                <Col span={2}> 站点选取 :</Col>
                                <Col span={7}>
                                <TreeSelect
                                    size="small"
                                    style={{ width: 300 }}
                                    treeData={treeData}
                               
                                    onChange={this.onTreeSelect}
                                    maxTagCount={2}
                                    treeCheckable={true}
                                    treeDataSimpleMode={true}

                                />
                               
                            </Col>
                            <Col span={6}><Tag color="#108ee9" onClick={this.onAdd}>确认添加</Tag></Col>
                            </Row>

                    </p>

                    <MyTable
                        // bordered
                        loading={loading}
                        columns={columns}
                        // rowSelection={rowSelection}
                        pagination={true}
                        scroll={{ y: windowWidth < 1400 ? height - 280 : height - 300 }}
                        dataSource={PublicService.transformArrayData(dataSource, true, true, pageIndex, pageSize)}
                    />

                    

                    <Button type="primary" onClick={this.save} className={style.Btn_bc}>保存</Button>



                </Container>
            </div>
        )
    }
}
export default (WordEdit);
